class ResourcesController < ApplicationController
  layout 'resource_viewer'
  def index
    @resources = []
    Dir.glob("app/models/*rb") do |f|
      f.match(/\/([a-z_]+).rb/)
      classname = $1.camelize
      klass = Kernel.const_get classname
      @resources << klass if klass.superclass == ActiveRecord::Base
    end
    
    respond_to do |wants|
      wants.html {}
      wants.json do 
        hash = @resources.map{ |r|
          {
            :name => r.to_s,
            :relationships => r.reflect_on_all_associations.map { |a|
              {
                :macro   => a.macro.to_s,
                :related => a.class_name.to_s.camelize.singularize
              }
            },
            :columns => r.columns.map { |c|
              {
                :type => c.type.to_s,
                :name => c.name.to_s
              }
            }
          }
        }
        render :json => {:resources => hash}.to_json
      end
    end
  end
end
